package com.wzp.yyptproject.until;

import com.wzp.yyptproject.entity.ImgName;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import sun.net.www.protocol.ftp.FtpURLConnection;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class Controller {


    public static void main(String[] args) throws Exception {
         ImgName imgName = new ImgName();
        String imgname= imgName.getImgname();
        System.out.println("哈哈"+imgname);
        Controller c = new Controller();
        String url = "ftp://192.168.195.1:21/yypt/"+imgname;
        String path = "D:/asdasd.JPG";
        String str = c.GetImageStrFromUrl(url);    //读取输入流,转换为Base64字符
        System.out.println(str);
//        generateImage(str, path);            //将Base64字符转换为图片


    }


    /**
     * 读取输入流,转换为Base64字符串
     *
     * @return
     */
    public static String GetImageStrFromUrl(String imgURL) {
        byte[] data = null;
        try {
            URL url = new URL(imgURL);
            FtpURLConnection conn = (FtpURLConnection)url.openConnection();
//            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            //每次读取的字符串长度，如果为-1，代表全部读取完毕
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            data = outStream.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
        }
        String base64Image = Base64.getEncoder().encodeToString(data);
        return base64Image;
    }


    /**
     * 图片转化成base64字符串 将图片文 件转化为字节数组字符串，并对其进行Base64编码处理
     *
     * @return
     */
    public static String GetImageStr(File file) {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    /**
     * base64字符串转化成图片 对字节数组字符串进行Base64解码并生成图片
     *
     * @param imgStr 数据内容(字符串)
     * @param path   输出路径
     * @return
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(imgStr);// Base64解码
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
